<template>
  <div class="header">
    <!-- 折叠按钮 -->
    <div class="collapse-btn" @click="collapseChange">
      <i class="el-icon-s-unfold"></i>
    </div>
    <div class="logo">MyMall后台管理</div>
    <div class="header-right">
      <div class="header-user-con">
        <!-- 用户头像 -->
        <div class="user-avator">
          <el-avatar :src="user.avatarUrl"></el-avatar>
        </div>
        <!-- 用户名下拉菜单 -->
        <el-dropdown class="user-name" trigger="click" @command="handleCommand">
          <span class="el-dropdown-link">
            {{ user.nickname }}
            <i class="el-icon-caret-bottom"></i>
          </span>
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item command="logout">退出登录</el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
      </div>
    </div>
  </div>
</template>
<script>
import _ctrEvent from '@/assets/js/ctrEvent.js'
import { mixin } from '@/mixins'
export default {
  mixins: [mixin],
  data() {
    return {
      collapse: true,
      user: { }
    }
  },
  mounted() {
    this.user = JSON.parse(localStorage.getItem('USER_INFO'))
    console.log('user: ' + JSON.stringify(this.user))
    if (document.body.clientWidth < 1500) {
      this.collapseChange()
    }
  },
  methods: {
    // 用户名下拉菜单选择事件
    handleCommand(command) {
      if (command === 'logout') {
        localStorage.removeItem('USER_INFO')
        this.$router.push('/')
      }
    },
    // 侧边栏折叠
    collapseChange() {
      this.collapse = !this.collapse
      _ctrEvent.$emit('collapse', this.collapse)
    }
  }
}
</script>
<style scoped>
.header {
  position: absolute;
  z-index: 100;
  box-sizing: border-box;
  width: 100%;
  height: 70px;
  font-size: 22px;
  color: #b0b3b2;
  background: #1a1b1c;
  box-shadow: 0px 0px 8px 2px rgba(0, 0, 0, 0.3);
}

.collapse-btn {
  float: left;
  padding: 0 21px;
  cursor: pointer;
  line-height: 70px;
}

.header .logo {
  float: left;
  width: 250px;
  font-family: 26px;
  line-height: 70px;
  font-style: italic;
  font-weight: bold;
}

.header-right {
  float: right;
  padding-right: 50px;
}

.header-user-con {
  display: flex;
  height: 70px;
  align-items: center;
}

.user-name {
  margin-left: 10px;
}

.user-avator {
  margin-left: 20px;
}

.el-dropdown-link {
  color: #b0b3b2;
  cursor: pointer;
}

.el-dropdown-menu__item {
  text-align: center;
}
</style>
